package cn.com.anypay.manager.common.request;

import lombok.Data;

import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.Max;

/**
 * 分页请求封装类
 * 统一处理分页参数
 */
@Data
public class PageRequest {

    /**
     * 页码，从1开始
     */
    @Min(value = 1, message = "页码不能小于1")
    private Integer pageNumber = 1;

    /**
     * 页大小
     */
    @Min(value = 1, message = "页大小不能小于1")
    @Max(value = 500, message = "页大小不能超过500")
    private Integer pageSize = 10;

    /**
     * 排序字段
     */
    private String sortField;

    /**
     * 排序方向：asc 或 desc
     */
    private String sortDirection = "desc";

    /**
     * 搜索关键词
     */
    private String keyword;

    public PageRequest() {}

    public PageRequest(Integer pageNumber, Integer pageSize) {
        this.pageNumber = pageNumber != null && pageNumber > 0 ? pageNumber : 1;
        this.pageSize = pageSize != null && pageSize > 0 ? pageSize : 10;
    }

    public PageRequest(Integer pageNumber, Integer pageSize, String sortField, String sortDirection) {
        this(pageNumber, pageSize);
        this.sortField = sortField;
        this.sortDirection = sortDirection != null && !sortDirection.isEmpty() ? sortDirection : "desc";
    }

    /**
     * 获取偏移量 (用于数据库查询)
     */
    public long getOffset() {
        return (long) (pageNumber - 1) * pageSize;
    }

    /**
     * 是否升序排序
     */
    public boolean isAsc() {
        return "asc".equalsIgnoreCase(sortDirection);
    }

    /**
     * 是否降序排序
     */
    public boolean isDesc() {
        return "desc".equalsIgnoreCase(sortDirection);
    }
}